package com.yuezhu.com_volley;

import android.content.Context;

import com.android.volley.toolbox.HurlStack;
import com.yuezhu.global.ApplicationContext;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

/**
 * 重新定义HttpUrlConnection的获取方式（请阅读volley源码，慎重修改）
 */
public class OkHurlStack extends HurlStack {

    @Override
    protected HttpURLConnection createConnection(URL url) throws IOException {
        HttpURLConnection httpURLConnection = null;
        Context _con = ApplicationContext.getContext();
        ConnectManager conn = new ConnectManager(_con);
        if (conn.isWapNetwork()) {
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(conn.getProxy(), Integer.parseInt(conn.getProxyPort())));
            httpURLConnection = (HttpURLConnection)url.openConnection(proxy);
        } else {
            httpURLConnection = (HttpURLConnection) url.openConnection();
        }

        return httpURLConnection;
    }
}
